package cn.tb.basic.util;

import cn.tb.basic.jwt.JwtUtils;
import cn.tb.basic.jwt.LoginResult;
import cn.tb.basic.jwt.Payload;
import cn.tb.basic.jwt.RsaUtils;
import cn.tb.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * 登录上下文
 * 1.当前登录人信息====
 *
 * 2.当前登录人拥有的权限
 *
 */
public class LoginContext {

    /**
     * 获取当前登录人
     * @param request
     * @return
     */
    public static Logininfo getCurrentLogininfo(HttpServletRequest request){
        //1.从请求头中获取 U-TOKEN
        String token = request.getHeader("token");//JWT 串
        //获取公钥
        PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
        //使用公钥进行解密jwt串
        Payload<LoginResult> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginResult.class);
        LoginResult loginResult = payload.getUserInfo();
        return loginResult.getLogininfo();
    }
}
